﻿/////////////////////////////////////////////////
//文件名:Children
//描  述:
//创建者:刘凡平(Iveely Liu)
//邮  箱:945222889@qq.com
//组  织:Iveely
//年  份:2012/5/11 13:50:28
///////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;
using IveelySE.BigData.Common;

namespace IveelySE.BigData.MasterNode
{
    /// <summary>
    /// 存储列表类
    /// </summary>
    public class StoreList
    {
        /// <summary>
        /// 存储列表
        /// </summary>
        private static List<StoreNode> List = new List<StoreNode>();

        /// <summary>
        /// 添加结点
        /// </summary>
        /// <param name="node">需要添加的结点</param>
        private void Add(StoreNode node)
        {
            List.Add(node);
        }

        public static PObject dealWith(byte[] msg)
        {
            return new PObject("OK", "OK");
        }

        /// <summary>
        /// 存储结点上线监听端口
        /// </summary>
        public void Listen()
        {
            byte[] msg=new byte[10240];
            BigData.Common.Communication.Transfer.Server storeNode = new Common.Communication.Transfer.Server(Program.Config.IP, Program.Config.OnLineListenPort);
            //一直监听
            while (true)
            {
                storeNode.Listen(ref msg,dealWith);
                string message = ((PObject)Common.Serialize.Byte2Obj(msg)).value.ToString();
                string[] info = message.Split(',');
                StoreNode node = new StoreNode()
                {
                    IP = IPAddress.Parse(info[0]),
                    IsActivity = true,
                    WritePort = int.Parse(info[1]),
                    ReadPort = 32
                };
                Common.Printf.WriteInfo(node.IP+"存储结点上线。");
                Add(node);
            }
        }

        /// <summary>
        /// 禁用某个存储结点
        /// </summary>
        /// <param name="ip">存储结点的IP</param>
        public void Ban(string ip)
        {

        }

        /// <summary>
        /// 选择发送结点
        /// </summary>
        /// <param name="msg">需要发送的消息</param>
        /// <returns>返回发送的结点</returns>
        public StoreNode ChoseNode(PObject obj)
        {
           // this.AddFileInfo(name);
            return List[obj.Id%List.Count];
        }

        /// <summary>
        /// 增加文件状态信息
        /// </summary>
        //private void AddFileInfo(string name)
        //{
        //    if (Files.Contains(name))
        //    {
        //        this.Files[name] = int.Parse(this.Files[name].ToString()) + 1;
        //    }
        //    else
        //    {
        //        this.Files.Add(name, 0);
        //    }
        //}

        ///// <summary>
        ///// 获取某个文件的当前行号
        ///// </summary>
        ///// <param name="fileName"></param>
        ///// <returns></returns>
        //public int GetRowNum(string fileName)
        //{
        //    //int index=this.Files[fileName] != null ? int.Parse(this.Files[fileName].ToString()) : 0;
        //    return index % List.Count;
        //}
    }
}
